#!/usr/bin/perl

if(! -x "/usr/bin/cflow"){print "\'cflow\' not installed.\n"; exit;}

$in=join " ",@ARGV;
my @color=qw(#eecc80 #ccee80 #80ccee #eecc80 #80eecc #eecc80 #ccee80 #80ccee #eecc80 #80eecc #eecc80 #ccee80 #80ccee #eecc80 #80eecc #eecc80 #ccee80 #80ccee #eecc80 #80eecc);
my @shape=qw(box ellipse octagon hexagon diamond box ellipse octagon hexagon diamond box ellipse octagon hexagon diamond box ellipse octagon hexagon diamond);
my $pref="/tmp/cflow";
my $ext="svg";
my $narr=@shape;

foreach (`/usr/bin/cflow -l $in`){
	chomp;
	s/\(.*$//; s/^\{\s*//; s/\}\s*/\t/;
	my($n,$f)=split /\t/,$_;
	$index[$n]=$f;
	if($n){
		$_="$index[$n-1]\->$f";
		if ($narr > $n) {
			push @output,"node [color=\"$color[$n-1]\" shape=$shape[$n]];edge [color=\"$color[$n-1]\"]; $_;\n" if(! $count{$_}++);
		} else {
			push @output,"node [color=\"#80ccee\" shape=box];edge [color=\"#80ccee\"]; $_;\n" if(! $count{$_}++);
		}
	} else {
		push @output,"$f [shape=box];\n";
	}
}
unshift @output,"digraph G {\nnode [peripheries=2 style=\"filled,rounded\" fontname=\"Vera Sans YuanTi Mono\" color=\"$color[0]\"];\nrankdir=LR;\n";
push @output,"}\n";
print @output;
